package Java测试.编码3;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Chat {
    public static void main(String[] args) throws Exception{
        Scanner scanner = new Scanner(System.in);
        Socket socket = new Socket("192.168.3.172", 8888);
        while (true){
            //发送数据
            OutputStream os = socket.getOutputStream();
            String s = scanner.next();
            byte[] bytes = s.getBytes();
            os.write(bytes);
            if (s.equals("bye")){
                break;
            }
            //接收数据
            InputStream is = socket.getInputStream();
            byte[] bytes1 = new byte[1024];
            int read = is.read(bytes);
            String s1 = new String(bytes, 0, read);
            System.out.println("接收的数据为"+s1);
            if (s1.equals("bye")){
                break;
            }
        }
        socket.close();
    }
}
